package day6;

import java.util.Scanner;

public class Market {
    Scanner in = new Scanner(System.in);
    String name, adress;
    Card memb[] = new Card[10];
    int count = 0;

    public boolean addinfo(Card c) {
        if (count < memb.length) {
            if (checkCardNoRepeat(c.getCardNo())) {
                return false;
            } else {
                memb[count] = c;
                count++;
                return true;
            }
        } else {
            this.incMemb(c);
        }
        return true;
    }

    public boolean incMemb(Card c) {
        Card[] memb2 = new Card[memb.length + 10];
        if (count >= memb.length) {
            for (int i = 0; i < memb.length; i++) {
                memb2[i] = memb[i];
            }
            if (!checkCardNoRepeat(c.getCardNo())) {
                memb2[count] = c;
                return true;
            }
            memb = memb2;
        }

        return false;
    }

    public void show() {
        for (int i = 0; i < count; i++) {
            memb[i].showcardinfo();
        }
    }

    public boolean checkCardNoRepeat(String no) {
        for (int i = 0; i < count; i++) {
            if ((memb[i].getCardNo()).equals(no)) {
                System.out.println("卡号已被使用，请重新添加");
                return true;
            }
        }
        return false;
    }

    public boolean inc(String cardNo, int fen) {
        for (int i = 0; i < count; i++) {
            if ((memb[i].getCardNo()).equals(cardNo)) {
                memb[i].incpoint(cardNo, fen);
                System.out.println("增加成功。");
                return true;
            }
            System.out.println(cardNo + "这张会员卡不存在！");
            return false;
        }
        return true;
    }

    public String exchange(String cardNo) {
        for (int i = 0; i < count; i++) {
            if (!((memb[i].getCardNo()).equals(cardNo))) {
                System.out.println("您输入的卡号不存在！");
                return null;
            } else {
                System.out.println("1：100分兑换餐巾纸一包；2：400分兑换剃须刀一把；3：1000分兑换炒锅一个；请选择：");
                int chosen = in.nextInt();
                switch (chosen) {
                    case 1: {
                        if (memb[i].getPoint() < 100) {
                            System.out.println("抱歉，您的积分不够！");
                            return null;
                        }
                        memb[i].exchangepoint(1);
                        System.out.println("使用100积分兑换餐巾纸一包！");
                        break;
                    }
                    case 2: {
                        if (memb[i].getPoint() < 400) {
                            System.out.println("抱歉，您的积分不够！");
                            return null;
                        }
                        memb[i].exchangepoint(2);
                        System.out.println("使用400积分兑换剃须刀一把！");
                        break;
                    }
                    case 3: {
                        if (memb[i].getPoint() < 1000) {
                            System.out.println("抱歉，您的积分不够!");
                            return null;
                        }
                        memb[i].exchangepoint(3);
                        System.out.println("使用1000积分兑换超过一个!");
                        break;
                    }
                    default: {
                        System.out.println("选择错误，请重新选择");
                        break;
                    }
                }

            }
        }
        return cardNo;
    }

    public void showMarketinfo() {
        System.out.println("这个超市名叫" + name + ",地址在" + adress + ",有会员" + count + "人。");
    }


}
